---
id: mariadb-declarative-sql
title: Getting started with SQL declarative migrations for a MariaDB schema
slug: /getting-started/mariadb-declarative-sql
---

import InstallationInstructions from '../../components/_installation_instructions.mdx'

### Introduction

Atlas makes it easy to manage your database using the declarative migrations workflow. Declarative migrations focus on describing the desired state of the database and allow the migration engine to plan and execute the necessary actions to reach that state.

In this quickstart guide, we will go through the three essential steps for using Atlas to manage your database schema: inspecting your database, authoring the desired changes, and applying those changes.

### Installing Atlas

<InstallationInstructions />

### Warming up
To demonstrate the examples in the article, let's start a docker container with an example database and create a table in it.

Use the following command to run a MariaDB Docker container:

```console
docker run --name atlas-demo -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=example -p 3306:3306 -d mariadb:latest
```

Once the container is running, you can connect to the MariaDB database using the command below:

```console
docker exec -it atlas-demo mariadb -ppass example
```

Once you're connected, you can create a table in it by running the following command:

```sql
CREATE TABLE customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(255) NOT NULL,
    last_name VARCHAR(255) NOT NULL
);
```

## Step 1: Inspecting the database

Before planning any changes to your database, it can be useful to know what your database schema looks like. To do this, you can use the `atlas schema inspect` command to inspect your database. This command generates a file that represents your database schema, which can be used as a starting point for making changes.

### Usage
```
atlas schema inspect [flags]
```

### Example

The following command will inspect the current state of the schema `example` and store it in a file named `schema.sql`

```console
atlas schema inspect \
  --url "mysql://root:pass@localhost:3306/example" \
  --format "{{ sql . }}" > schema.sql
```

```sql title="schema.sql"
-- create "customers" table
CREATE TABLE `customers` (
   `id` int NOT NULL AUTO_INCREMENT,
   `first_name` varchar(255) NOT NULL,
   `last_name` varchar(255) NOT NULL,
   PRIMARY KEY (`id`)
);
```

:::info
To learn more about inspecting schema with Atlas in variety of use cases, visit the documentation [here](/declarative/inspect)
:::

## Step 2: Planning a change

Once you have inspected your database, the next step is to plan a change. This is done by modifying the SQL file in a way that it defines the desired state of your schema. The file should include the changes you want to make to your database, such as creating tables, columns, or indexes.

:::info
In this article, we will be focusing on SQL-based schema inspection with Atlas. For those opting to use an HCL schema, read the documentation [here](/getting-started/mariadb-declarative-hcl).
:::

### Example

Let’s add the following table definition to the SQL schema file that we previously created with the `atlas schema inspect` command:

```sql title="schema.sql" {9-15}
-- create "customers" table
CREATE TABLE `customers` (
   `id` int NOT NULL AUTO_INCREMENT,
   `first_name` varchar(255) NOT NULL,
   `last_name` varchar(255) NOT NULL,
   PRIMARY KEY (`id`)
);

CREATE TABLE orders (
   id int NOT NULL AUTO_INCREMENT,
   customer_id int NOT NULL,
   order_date date NOT NULL,
   PRIMARY KEY (id),
   CONSTRAINT customer_id_fk FOREIGN KEY (customer_id) REFERENCES example.customers (id)
);
```

## Step 3: Applying the change

The final step is to apply the change to your database. To do this, simply run the `atlas schema apply` command. This command will compare the current state of your database to the desired state defined in your SQL file and make the necessary changes.

### Usage

```console
atlas schema apply [flags]
```

### Example

Let’s apply the changes we created in Step 2 using the following command:

```console
atlas schema apply \
  --url "mysql://root:pass@localhost:3306/example" \
  --to "file://schema.sql" \
  --dev-url "docker://maria/latest/example"
```

:::info The role of the [Dev Database](/concepts/dev-database)
Atlas loads the **desired state** by executing the SQL files onto the provided [dev database](/concepts/dev-database),
compares it against the database **current state** by inspecting its schema and writes a migration plan for moving
from the current state to the desired state.
:::

```console
-- Planned Changes:
-- Create "orders" table
// highlight-next-line-info
CREATE TABLE `orders` (`id` int NOT NULL AUTO_INCREMENT, `customer_id` int NOT NULL, `order_date` date NOT NULL, PRIMARY KEY (`id`), INDEX `customer_id_fk` (`customer_id`), CONSTRAINT `customer_id_fk` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT) CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
Use the arrow keys to navigate: ↓ ↑ → ← 
? Are you sure?: 
  ▸ Apply
    Abort
```

Selecting `Apply` will apply the changes to the database.

You can inspect the latest state of the database again and verify that the changes have been applied by re-running the `atlas schema inspect` command:

```console
atlas schema inspect \
  --url "mysql://root:pass@localhost:3306/example" \
  --format "{{ sql . }}"
```
```sql
-- create "customers" table
CREATE TABLE `customers` (
    `id` int NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(255) NOT NULL, 
    `last_name` varchar(255) NOT NULL, 
    PRIMARY KEY (`id`)
) CHARSET utf8mb4 COLLATE utf8mb4_general_ci;

-- create "orders" table
CREATE TABLE `orders` (
    `id` int NOT NULL AUTO_INCREMENT, 
    `customer_id` int NOT NULL, 
    `order_date` date NOT NULL, 
    PRIMARY KEY (`id`), 
    INDEX `customer_id_fk` (`customer_id`), 
    CONSTRAINT `customer_id_fk` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT
) CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
```
Great! Our changes have been successfully applied to the `example` schema.

## Conclusion

By following the steps above, you can inspect the current state of the database, make changes to the schema definition file and apply those changes to the database with confidence.

The declarative migration is a flexible way to manage your database schema. It allows you to define your desired state, and then automatically apply those changes to your database. This can save you time and effort, and also reduce the risk of errors or inconsistencies in your database schema.

:::info
Atlas supports both Declarative and Versioned workflows for managing database schemas. The choice between the two depends on your requirements. To understand the difference and choose the best option for you, visit the documentation on Declarative vs Versioned workflow [here](/concepts/declarative-vs-versioned).
:::

## Need More Help?

[Join the Ariga Discord Server](https://discord.com/invite/zZ6sWVg6NT) for early access to features and the ability to provide exclusive feedback that improves your Database Management Tooling. 

[Sign up](https://atlasnewsletter.substack.com/) to our newsletter to stay up to date about Atlas, and the cloud platform [Atlas Cloud](https://atlasgo.cloud).
